﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Property.Has
{
    [Serializable]
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Property)]
    [ProvideAspectRole(Roles.Conditional)]
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    public sealed class AnyAttribute : ComparingContractBase
    {
        private object[] _collection;

        public AnyAttribute(object[] collection)
        {
            if (collection == null) throw new ArgumentNullException();
            _collection = collection;
        }

        protected override bool ValidateProperty(object property)
        {
            var _property = property as System.Collections.IEnumerable;
            if (_property == null) throw new ArgumentException();
            foreach (var propertyItem in _property)
            {
                foreach (var collectionItem in _collection)
                {
                    if (Comparer.Compare(propertyItem, collectionItem) == 0) return true;
                }
            }
            return false;
        }
    }
}
